package com.prim.thread;

public class ThreadLocalTest {
    //每个线程都会有它的一个副本
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        threadLocal.set("mainThread");
        new Thread(new Runnable() {
            @Override
            public void run() {
                threadLocal.set("thread1");
                try {
                    Thread.sleep(1000L);
                    System.out.println("thread1 get:" + threadLocal.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                threadLocal.set("thread2");
                try {
                    Thread.sleep(2000L);
                    System.out.println("thread2 get:" + threadLocal.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        System.out.println("ThreadLocalTest.main:"+threadLocal.get());
    }
}
